package com.zsrt.jmj.student.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.jmj.student.entity.StudentEntity;
import com.zsrt.jmj.student.service.StudentService;
import com.zsrt.jmj.student.service.impl.StudentServiceImpl;
import com.zsrt.jmj.utils.GsonUtil;


public class StudentController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private StudentService service=new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	String method = req.getParameter("method");
    	
    	Map<String, Object> map = new HashMap<>();
    	if(!"".equals(method) && null != method){
    		if(method.equals("add")){
    			String studentName = req.getParameter("studentName");
    			String age = req.getParameter("age");
    			String educationId = req.getParameter("educationId");
    			String teacherId = req.getParameter("teacherId");
    			
    			StudentEntity entity  =  new StudentEntity();
    			entity.setStudentName(studentName);
    			entity.setAge(Integer.parseInt(age));
    			entity.setEducation(Integer.parseInt(educationId));
    			entity.setTeacherId(Integer.parseInt(teacherId));
    			service.add(entity,map);
    		}else if("findAllStudents".equals(method)){
    			service.findAllStudents(map);
    		}else if("findStudentById".equals(method)){
    			
    			String id = req.getParameter("id");
    			service.findStudentById(id,map);
    		}else if("updateById".equals(method)){
    			String id = req.getParameter("id");
    			String education = req.getParameter("education");
    			String teacherId = req.getParameter("teacherId");
    			String studentName = req.getParameter("studentName");
    			String age = req.getParameter("age");
    			
    			StudentEntity entity = new StudentEntity();
    			entity.setAge(Integer.parseInt(age));
    			entity.setStudentName(studentName);
    			entity.setEducation(Integer.parseInt(education));
    			entity.setTeacherId(Integer.parseInt(teacherId));
    			entity.setId(Integer.parseInt(id));
    			
    			service.updateById(entity,map);
    		}else if("deleteByIds".equals(method)){
    			String ids = req.getParameter("ids");
    			service.deleteByIds(ids,map);
    		}
    
    	}
    	
    	GsonUtil.printJson(resp, map);
    }
    

}
